<?php

namespace App\Http\Controllers\System;

use Illuminate\Http\Request;
use App\Models\System\SysLog;
use App\Models\System\Setting;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

/**
 * 系统设置
 *
 * Date: 2018/5/30
 * @author George
 * @package App\Http\Controllers\System
 */
class SettingController extends Controller
{
	/**
	 * 获取系统设置
	 *
	 * Date: 27/04/2018
	 * @author George
	 * @return \Illuminate\Http\JsonResponse
	 */
    public function index()
    {
    	$user = Auth::user();
    	$seeting = Setting::query()->where('company_id', $user->company_id)->first();
    	return success($seeting);
    }

	/**
	 * 获取注册设置
	 *
	 * Date: 2018/5/30
	 * @author George
	 * @param Request $request
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function register(Request $request)
	{
		$this->validate($request, [
			'company_id' => 'required'
		]);

		$setting = $this->setting($request->get('company_id'));
		$register = optional($setting)->register;
		return success($register ?: config('system.register'));
    }

	/**
	 * 获取考勤积分设置
	 *
	 * Date: 2018/5/30
	 * @author George
	 * @param Request $request
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function attendance(Request $request)
	{
		$this->validate($request, [
			'company_id' => 'required'
		]);

		$setting = $this->setting($request->get('company_id'));
		$attendace = optional($setting)->attendance;
		return success($attendace ?: config('system.attendance'));
    }

	/**
	 * 更新注册设置
	 *
	 * Date: 2018/5/30
	 * @author George
	 * @param Request $request
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \Throwable
	 */
	public function updateRegister(Request $request)
	{
		$attributes = $this->validate($request, [
			'stature' => 'required',
			'education' => 'required',
			'address' => 'required',
			'marital' => 'required',
			'urgency_name' => 'required',
			'urgency_mobile' => 'required',
			'urgency_relation' => 'required',
			'trade' => 'required',
			'skill' => 'required',
			'channel' => 'required',
		]);

		$user = Auth::user();
		$setting = Setting::query()->where('company_id', $user->company_id)->first();

		if ($setting) {
			$setting->register = $attributes;
			$setting->saveOrFail();
		} else {
			$this->create($user->company_id, $attributes, []);
		}
		SysLog::log($user,'注册设置','更新注册设置');
        return updated($attributes);
    }

	/**
	 * 更新考勤积分设置
	 *
	 * Date: 2018/5/30
	 * @author George
	 * @param Request $request
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \Throwable
	 */
	public function updateAttendance(Request $request)
	{
		$attributes = $this->validate($request, [
			'first' => 'required',
			'second' => 'required',
			'third' => 'required',
			'last' => 'required',
			'tardy' => 'required',
			'early' => 'required',
			'nsignout' => 'required',
			'absenteeism' => 'required',
		]);

		$user = Auth::user();
		$setting = Setting::query()->where('company_id', $user->company_id)->first();
		if ($setting) {
			$setting->attendance = $attributes;
			$setting->saveOrFail();
		} else {
			$this->create($user->company_id, [], $attributes);
		}
		SysLog::log($user,'考勤积分设置','更新考勤积分设置');
        return updated($attributes);
	}

	/**
	 * 获取系统设置
	 *
	 * Date: 2018/5/30
	 * @author George
	 * @param int $company_id
	 * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|null|object
	 */
	public function setting(int $company_id)
	{
		return Setting::query()->where('company_id', $company_id)->first();
    }

	/**
	 * 创建公司基础设置
	 *
	 * Date: 2018/6/28
	 * @author George
	 * @param int $company_id
	 * @param array $register
	 * @param array $attendance
	 * @return Setting|\Illuminate\Database\Eloquent\Model
	 */
	protected function create(int $company_id, array $register = [], array $attendance = [])
	{
		if (count($register) === 0) {
			$register = [
				'stature' => 'required',
				'education' => 'required',
				'address' => 'required',
				'marital' => 'required',
				'urgency_name' => 'required',
				'urgency_mobile' => 'required',
				'urgency_relation' => 'required',
				'trade' => 'required',
				'skill' => 'required',
				'channel' => 'required',
			];
		}

		if (count($attendance) === 0) {
			$attendance = [
				'first' => 5,
				'second' => 4,
				'third' => 3,
				'last' => -9,
				'tardy' => -2,
				'early' => -2,
				'nsignout' => -1,
				'absenteeism' => -10,
			];
		}

		return Setting::create([
			'company_id' => $company_id,
			'register' => $register,
			'attendance' => $attendance
		]);
    }
}
